package com.zgjkhis.modules.security.security;

import com.zgjkhis.modules.security.security.vo.JwtUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @ClassName LoginUserUtil
 * @Deacription TODO
 * @Author yzq
 * @Date 2021/6/30 11:36
 * @Version 1.0
 **/
public class LoginUserUtil {

    /**
     * 获取系统用户门店id
     *
     * @return 系统用户id
     */
    public static String getPharmId() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new SecurityException("当前登录状态过期");
        }
        JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
        return jwtUser.getPharmacyId();
    }

    /**
     * 获取系统用户门店id
     *
     * @return 系统用户id
     */
    public static String getNickName() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new SecurityException("当前登录状态过期");
        }
        JwtUser jwtUser = (JwtUser) authentication.getPrincipal();
        return jwtUser.getNickName();
    }
}
